package com.fancon.android.gardenjokesx.social;

import java.net.URL;

import android.content.Context;


import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.http.AccessToken;

public class TwitterAPI {
	private Twitter mTwitter;
	private TwitterSession mSession;
	private AccessToken mAccessToken;
	
	public TwitterAPI(Context context ){
		mTwitter 		= new TwitterFactory().getInstance();
		mSession		= new TwitterSession(context);
		mAccessToken	= mSession.getAccessToken();
	}
	
	public void resetAccessToken() {
		if (mAccessToken != null) {
			mSession.resetAccessToken();
		
			mAccessToken = null;
		}
	}
	
	public String getUsername() {
		return mSession.getUsername();
	}
	
	public void updateStatus(String status) throws Exception {
		try {
			mTwitter.updateStatus(status);
		} catch (TwitterException e) {
			throw e;
		}
	}
	
/**
 * Lay ImageProfile cua User, tra ve la URL
 * Su dung: 
 * 				URL url = mTwitter.getImageProfile();
				String link = url.getProtocol() + "://" + url.getHost() + "/"
						+ (url.getPath() == null ? "" : url.getPath())
						+ (url.getQuery() == null ? "" : "?" + url.getQuery());
				Log.v("url", link);
	
 * @return
 */
	public URL getImageProfile () {
		User user;
		URL url = null;
		try {
			user = mTwitter.showUser(mTwitter.getId());
			url =  user.getProfileImageURL();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TwitterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return url;
	}
	
	
}
